<?php

namespace app\api\service;

use think\facade\Db;

class ResellService
{

    /**
     *
     * 获取我的转卖
     * */

    public static function getUserResellList($user_id,$resell_status)
    {
        $query = Db::name('user_resell_goods')
                        ->alias('a')
                        ->field('b.resell_goods_id,a.goods_name,a.attr,a.image,a.origin_price,a.is_resell_coupon,a.resell_times,
                        a.single_resell_time,a.resell_price,a.goods_num,a.group_num,b.resell_status,b.status,b.end_time')
                        ->join('user_resell_session b','a.id=b.resell_goods_id')
                        ->withAttr('end_time',function ($value){
                            return date('Y-m-d H:i:s',$value);
                        })->where('a.user_id',$user_id)
                        ->order('b.end_time','asc');

        if (!empty($resell_status)){
            $query->where('b.status',$resell_status);
        }

        $goodsList = $query->paginate(10);

        return $goodsList;
    }


    /**
     *
     * 获取我的仓库
     * */

    public static function getUserRepositoryList($user_id,$resell_status)
    {
        $query = Db::name('user_resell_goods')
            ->field('id as resell_goods_id,goods_name,attr,image,origin_price,is_resell_coupon,resell_times,
                        single_resell_time,resell_price,goods_num,group_num,resell_status')
            ->where('user_id',$user_id)
            ->order('id','asc');

        if (!empty($resell_status)){
            $query->where('resell_status',$resell_status);
        }

        $goodsList = $query->paginate(10);

        return $goodsList;
    }



    /**
     *
     * 获取转卖商品信息列表
     * */
    public static function getResellGoodsList($store_id)
    {
        $goodsList = Db::name('user_resell_goods')
                    ->alias('a')
                    ->field('b.resell_goods_id,a.goods_name,a.image,a.group_activity_id,a.group_goods_id,a.attr,a.origin_price,
                    a.resell_price,b.end_time')
                    ->join('user_resell_session b','a.id=b.resell_goods_id')
                    ->where('a.store_id',$store_id)
                    ->withAttr('end_time',function ($value){
                        return date('Y-m-d H:i:s',$value);
                    })->whereBetweenTimeField('b.start_time','b.end_time')
                    ->paginate();

        return $goodsList;
    }

    /**
     *
     * 获取转卖商品信息列表
     * */
    public static function findResellGoodsDetail($id)
    {
        $goods = Db::name('group_goods')
                    ->alias('a')
                    ->field('a.goods_name,a.spec_type,a.main_image,a.goods_images,a.goods_attr,d.content,b.group_activity_id,b.group_goods_id,b.attr,b.origin_price,
                            b.resell_price,b.goods_num,b.group_num,b.is_limit_member,c.end_time,c.resell_goods_id')
                    ->join('user_resell_goods b','a.id=b.group_goods_id')
                    ->join('user_resell_session c','b.id=c.resell_goods_id')
                    ->join('group_goods_content d','a.id=d.group_goods_id')
                    ->withAttr('end_time',function ($value){
                        return date('Y-m-d H:i:s',$value);
                    })
                    ->withAttr('main_image',function ($value){
                        return request()->domain() . $value;
                    })->withAttr('goods_attr',function ($value){
                        if(empty($value)){
                            return [];
                        }
                        return json_decode($value,true);
                    })->withAttr('goods_images',function ($value){
                        $imgs = [];
                        $arr = json_decode($value,true);
                        foreach ($arr as $v){
                            $imgs[] = request()->domain() . $v;
                        }
                        return $imgs;
                    })->whereBetweenTimeField('c.start_time','c.end_time')->where('b.id',$id)
                    ->find();

        $items = [];
        if ($goods['spec_type']==2){
                $items = Db::name('group_goods_item')
                    ->field('id,attr,image,market_price,sell_price,group_price,stock')
                    ->withAttr('image',function ($value){
                        return request()->domain() . $value;
                    })->where('group_goods_id',$goods['group_goods_id'])
                    ->select();
        }

        $goods['items'] = $items;

        return $goods;
    }


    /**
     *
     * 获取商品转卖卷
     * */
    public static function findResellCoupon($user_id)
    {
        $user_coupon_id = Db::name('user_coupon')
                            ->where('coupon_type',4)
                            ->whereBetweenTimeField('start_time','end_time')
                            ->where('user_id',$user_id)
                            ->order('end_time','asc')
                            ->value('id');

        return $user_coupon_id;
    }


    /**
     *
     * 获取转卖商品信息
     * */
    public static function findResellGoods($id)
    {
        $goods = Db::name('user_resell_goods')
            ->field('is_resell,is_resell_coupon,resell_times,store_id')
            ->where('id',$id)
            ->find();

        return $goods;
    }


    /**
     *
     * 新增转售信息
     * */
    public static function addResellRecord($resell_goods_id,$single_resell_time,$store_id)
    {
        $result = Db::name('user_resell_session')
                    ->insert([
                        'resell_goods_id' => $resell_goods_id,
                        'start_time' => time(),
                        'end_time' => time() + $single_resell_time*3600,
                        'store_id' => $store_id,
                        'status' => 1,
                        'resell_status' => -1,
                    ]);

        return $result;
    }

    /**
     *
     * 获取转手次数
     * */
    public static function getResellCount($resell_goods_id)
    {
        $count = Db::name('user_resell_session')->where('resell_goods_id',$resell_goods_id)->count('id');

        return $count;
    }


    /**
     *
     * 获取转卖商品信息
     * */
    public static function changeCouponStatus($id)
    {
        $result = Db::name('user_coupon')->where('id',$id)->update(['coupon_status'=>2]);

        return $result;
    }

    /**
     *
     * 获取转卖商品信息
     * */
    public static function changeResellGoodsStatus($id)
    {
        $result = Db::name('user_resell_goods')->where('id',$id)->update(['resell_status'=>2]);

        return $result;
    }


    /**
     * 领取商品
     * */
    public static function receiveGroupGoods($user_id,$goods)
    {

          $resell_rate = (float)$goods['resell_rate']/100;
          $resell_price = number_format( (float)$goods['group_price']* $resell_rate,2);
          $res = Db::name('user_resell_goods')->insert([
                  'user_id' => $user_id,
                  'store_id' => $goods['store_id'],
                  'group_activity_id' => $goods['group_activity_id'],
                  'group_goods_id' => $goods['group_goods_id'],
                  'group_order_id' => $goods['group_order_id'],
                  'goods_name' => $goods['goods_name'],
                  'image' => $goods['image'],
                  'attr' => $goods['attr'],
                  'origin_price' => $goods['group_price'],
                  'resell_price' => $resell_price,
                  'goods_num' => $goods['buy_num'],
                  'group_num' => $goods['transfer_people_num'],
                  'is_resell' => $goods['is_transfer'],
                  'is_limit_member' => $goods['is_limit_member'],
                  'resell_times' => $goods['transfer_times'],
                  'single_resell_time' => $goods['resell_time'],
                  'is_resell_coupon' => $goods['is_resell_coupon'],
                  'resell_status' => 0,
                  'receive_time' => time()
          ]);

          return $res;
    }

    /**
     * 获取订单信息
     * */
    public static function findGroupOrder($group_order_id)
    {
       $groupOrder =  Db::name('group_order')
                        ->field('prepayment_amount,real_amount')
                        ->where('group_order_id',$group_order_id)
                        ->find();

       return $groupOrder;
    }

    /**
     *
     * 查询该商品是否已被领取
     * */
    public static function findResellGoodsExists($group_order_id)
    {
       $resell_goods_id =  Db::name('user_resell_goods')->where('group_order_id',$group_order_id)->value('id');

       return $resell_goods_id;
    }


    /**
     *
     * 根据订单id获取详情的活动及商品信息
     * */
    public static function findGroupActivityGoods($group_order_id)
    {
        $groupActivityGoods = Db::name('group_order_item')
                    ->alias('a')
                    ->field('a.group_price,a.attr,a.goods_name,a.image,a.buy_num,a.group_goods_id,a.group_order_id,b.is_transfer,b.transfer_people_num,b.transfer_times,b.is_resell_coupon,
                            b.resell_rate,b.store_id,b.is_limit_member,b.resell_time,c.prepayment_amount,d.group_activity_id')
                    ->join('group_activity_record d','a.group_order_id=d.group_order_id')
                    ->join('group_activity b','b.id=d.group_activity_id')
                    ->join('group_order c','a.group_order_id=c.id')
                    ->where([
                        'a.group_order_id' => $group_order_id,
                    ])
                    ->find();

        return $groupActivityGoods;
    }

}